﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="DBAdmin.aspx.cs" Inherits="ElectronicJournal.Admin.DBAdmin" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    <asp:SqlDataSource ID="SqlDataSourceScientificDirections" runat="server"
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
        SelectCommand="SELECT * FROM ScientificDirections"
        UpdateCommand="UPDATE ScientificDirections SET Title = @Title WHERE Id = @Id"
        InsertCommand="INSERT INTO ScientificDirections VALUES (@Title, NEWID())">
        <InsertParameters>
            <asp:Parameter Name="Title" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Title" />
            <asp:Parameter Name="Id" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSourceNews" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
        SelectCommand="SELECT * FROM news ORDER BY date DESC"
        UpdateCommand="UPDATE news SET title = @title, content = @content WHERE idnews = @idnews"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSourcePublicationTypes" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
        SelectCommand="SELECT * FROM PublicationTypes"
        UpdateCommand="UPDATE PublicationTypes SET Title = @Title WHERE Id= @Id"
        InsertCommand="INSERT INTO PublicationTypes VALUES (@Title, NEWID())">
        <InsertParameters>
            <asp:Parameter Name="Title" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSourceUsersInRoles" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
        SelectCommand="SELECT aspnet_Users.UserId, aspnet_Users.UserName, aspnet_Membership.Email, aspnet_Roles.RoleId, aspnet_Roles.RoleName FROM aspnet_Users, aspnet_Membership, aspnet_Roles, aspnet_UsersInRoles WHERE aspnet_Membership.UserId = aspnet_Users.UserId AND aspnet_UsersInRoles.UserId = aspnet_Users.UserId AND aspnet_Roles.RoleId = aspnet_UsersInRoles.RoleId"
        UpdateCommand="UPDATE aspnet_UsersInRoles SET RoleId = @RoleId WHERE aspnet_UsersInRoles.UserId = @UserId">
        </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSourceRoles" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
        SelectCommand="SELECT * FROM aspnet_Roles">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSourceMembership" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
        SelectCommand="SELECT aspnet_Users.UserName, aspnet_Membership.UserId, aspnet_Membership.Email, aspnet_Membership.LastLoginDate, FROM aspnet_Users, aspnet_Membership WHERE aspnet_Users.UserId = aspnet_Membersip.UserId">
    </asp:SqlDataSource>
<a href="Default.aspx">Меню администратора</a>
<br /><br />
<table>
    <tbody>
        <tr>
            <td>
                <a id="ScientificDirectionsPanelToggle" style="cursor:pointer">Таблица научных направлений</a>
                <div style="display:none" id="ScientificDirectionsPanel">
                    <asp:GridView ID="GridViewScientificDirections" runat="server" CellPadding="4" 
                        DataSourceID="SqlDataSourceScientificDirections" ForeColor="#333333"
                        AutoGenerateColumns="False" DataKeyNames="Id">
                        <AlternatingRowStyle BackColor="White" />
                        <FooterStyle BackColor="#009999" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#009999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        <Columns>
                            <asp:BoundField DataField="Id" SortExpression="Id" Visible="false" />
                            <asp:BoundField DataField="Title" SortExpression="Title" HeaderText="Научное направление" />
                            <asp:CommandField ShowEditButton="true" EditText="Изменить" UpdateText="Применить" CancelText="Отмена" />
                        </Columns>
                    </asp:GridView>
				    <br />
                    <table>
                        <tbody>
                            <tr>
                                <td>
                                    Начное направление:
                                </td>
                                <td>
                                    <asp:TextBox ID="ScientificDirectionTextBox" runat="server" Width="300px" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Button runat="server" Text="Добавить" OnClick="AddScientificDirection" />
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <a id="NewsPanelToggle" style="cursor:pointer">Таблица новостей</a>
                <div style="display:none" id="NewsPanel">
                    <asp:GridView ID="GridView1" runat="server" CellPadding="4" 
                        DataSourceID="SqlDataSourceNews" ForeColor="#333333"
                        AutoGenerateColumns="False" DataKeyNames="idnews">
                        <AlternatingRowStyle BackColor="White" />
                        <FooterStyle BackColor="#009999" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#009999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        <Columns>
                            <asp:BoundField DataField="idnews" SortExpression="idnews" Visible="false" />
                            <asp:BoundField DataField="title" SortExpression="title" HeaderText="Заголовок" />
                            <asp:BoundField DataField="content" SortExpression="content" HeaderText="Новость" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="NewsDateLabel" Text='<%# Bind("date") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="true" EditText="Изменить" UpdateText="Применить" CancelText="Отмена" />
                        </Columns>
                    </asp:GridView>
				    <br />
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <a id="PublicationTypesPanelToggle" style="cursor:pointer">Таблица типов публикаций</a>
                <div style="display:none" id="PublicationTypesPanel">
                    <asp:GridView ID="GridViewPublicationTypes" runat="server" CellPadding="4" 
                        DataSourceID="SqlDataSourcePublicationTypes" ForeColor="#333333"
                        AutoGenerateColumns="False" DataKeyNames="Id">
                        <AlternatingRowStyle BackColor="White" />
                        <FooterStyle BackColor="#009999" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#009999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        <Columns>
                            <asp:BoundField DataField="Id" SortExpression="Id" Visible="false" />
                            <asp:BoundField DataField="Title" SortExpression="Title" HeaderText="Тип публикации" />
                            <asp:CommandField ShowEditButton="true" EditText="Изменить" UpdateText="Применить" CancelText="Отмена" />
                        </Columns>
                    </asp:GridView>
				    <br />
                    <table>
                        <tbody>
                            <tr>
                                <td>
                                    Тип публикации:
                                </td>
                                <td>
                                    <asp:TextBox ID="PublicationTypeTextBox" runat="server" Width="300px" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Button runat="server" Text="Добавить" OnClick="AddPublicationType" />
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <a id="UsersRolesPanelToggle" style="cursor:pointer">Таблица пользователей</a>
                <div style="display:none" id="UsersRolesPanel">
                    <asp:GridView ID="UsersInRolesGridView" runat="server" 
                    DataSourceID="SqlDataSourceUsersInRoles" AutoGenerateColumns="false" CellPadding="1" CellSpacing="1" DataKeyNames="UserId">
                        <AlternatingRowStyle BackColor="White" />
                        <FooterStyle BackColor="#009999" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#009999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#EFF3FB" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        <Columns>
                            <asp:BoundField HeaderText="UserId" SortExpression="UserId" DataField="UserId" Visible="false" />
                            <asp:BoundField HeaderText="UserName" SortExpression="UserName" DataField="UserName" />
                            <asp:BoundField HeaderText="Email" SortExpression="Email" DataField="Email" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Label runat="server" ID="RoleNameLabel" Text='<%# Bind("RoleName") %>' />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:DropDownList runat="server" ID="RolesDropDownList" DataSourceID="SqlDataSourceRoles" DataValueField="RoleId" DataTextField="RoleName" SelectedValue='<%# Bind("RoleId") %>'/>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="true" EditText="Изменить" UpdateText="Применить" CancelText="Отмена" />
                        </Columns>
                    </asp:GridView>
				</div>
            </td>
        </tr>
    </tbody>
</table>
<script>
    document.getElementById('ScientificDirectionsPanel').style.display = get_cookie('ScientificDirectionsPanel');
    document.getElementById('NewsPanel').style.display = get_cookie('NewsPanel');
    document.getElementById('PublicationTypesPanel').style.display = get_cookie('PublicationTypesPanel');
    document.getElementById('UsersRolesPanel').style.display = get_cookie('UsersRolesPanel');
    
    $("#UsersPanelToggle").click(function () {
        $("#UsersPanel").slideToggle("slow", function () {
            var expires = new Date();
            expires.setTime(expires.getTime() + (1000 * 86400 * 365));
            set_cookie('UsersPanel', document.getElementById('UsersPanel').style.display, expires, "/");
        });
    });

    $("#ScientificDirectionsPanelToggle").click(function () {
        $("#ScientificDirectionsPanel").slideToggle("slow", function () {
            var expires = new Date();
            expires.setTime(expires.getTime() + (1000 * 86400 * 365));
            set_cookie('ScientificDirectionsPanel', document.getElementById('ScientificDirectionsPanel').style.display, expires, "/");
        });
    });

    $("#NewsPanelToggle").click(function () {
        $("#NewsPanel").slideToggle("slow", function () {
            var expires = new Date();
            expires.setTime(expires.getTime() + (1000 * 86400 * 365));
            set_cookie('NewsPanel', document.getElementById('NewsPanel').style.display, expires, "/");
        });
    });


    $("#PublicationTypesPanelToggle").click(function () {
        $("#PublicationTypesPanel").slideToggle("slow", function () {
            var expires = new Date();
            expires.setTime(expires.getTime() + (1000 * 86400 * 365));
            set_cookie('PublicationTypesPanel', document.getElementById('PublicationTypesPanel').style.display, expires, "/");
        });
    });

    $("#UsersRolesPanelToggle").click(function () {
        $("#UsersRolesPanel").slideToggle("slow", function () {
            var expires = new Date();
            expires.setTime(expires.getTime() + (1000 * 86400 * 365));
            set_cookie('UsersRolesPanel', document.getElementById('UsersRolesPanel').style.display, expires, "/");
        });
    });

    function get_cookie(name) {
        cookie_name = name + "=";
        cookie_length = document.cookie.length;
        cookie_begin = 0;
        while (cookie_begin < cookie_length) {
            value_begin = cookie_begin + cookie_name.length;
            if (document.cookie.substring(cookie_begin, value_begin) == cookie_name) {
                var value_end = document.cookie.indexOf(";", value_begin);
                if (value_end == -1) {
                    value_end = cookie_length;
                }
                return unescape(document.cookie.substring(value_begin, value_end));
            }
            cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
            if (cookie_begin == 0) {
                break;
            }
        }
        //return null;
        return "none";  //в случае отсутствия cookie возвращается значение по умолчанию "элемент не отображается"
    }

    function set_cookie(name, value, expires, path, domain, secure) {
        document.cookie = name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure=" + secure : "");
    }
</script>
</asp:Content>
